﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
using System.Drawing;

namespace PdfPreView
{
    class PrintImageHelper
    {
        public int printNum = 0;//多页打印

        public bool isSucPrint = false;

        #region 根据图片路径打印

        public string imageFile = "";//单个图片文件
        public ArrayList fileList = new ArrayList();//多个图片文件
        public void PrintDoc(bool isPreview = false)
        {
            PrintDocument docToPrint = new PrintDocument();
            docToPrint.BeginPrint += new PrintEventHandler(this.docToPrint_BeginPrint);
            docToPrint.EndPrint += new PrintEventHandler(this.docToPrint_EndPrint);
            docToPrint.PrintPage += new PrintPageEventHandler(this.docToPrint_PrintPage);
            docToPrint.DefaultPageSettings.Landscape = false;

            var printDialog = new PrintDialog();
            printDialog.AllowSomePages = true;
            printDialog.ShowHelp = true;
            printDialog.Document = docToPrint;
            if (isPreview)
            {
                if (printDialog.ShowDialog() == DialogResult.OK)
                {
                    docToPrint.Print();
                }
            }
            else
            {
                docToPrint.Print();
            }
        }
        private void docToPrint_BeginPrint(object sender, PrintEventArgs e)
        {
            if (fileList.Count == 0)
                fileList.Add(imageFile);
        }
        private void docToPrint_EndPrint(object sender, PrintEventArgs e)
        {

        }
        private void docToPrint_PrintPage(object sender, PrintPageEventArgs e)
        {
            //图片抗锯齿
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            using (Stream stream = File.OpenRead(fileList[printNum].ToString().Trim()))
            {
                using (Stream fs = FileStream.Synchronized(stream))
                {
                    try
                    {
                        System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
                        int x = 10;
                        int y = 10;
                        int width = image.Width;
                        int height = image.Height;
                        if ((width / e.PageBounds.Width) > (height / e.PageBounds.Height))
                        {
                            width = e.PageBounds.Width;
                            height = image.Height * e.PageBounds.Width / image.Width;
                        }
                        else
                        {
                            height = e.PageBounds.Height;
                            width = image.Width * e.PageBounds.Height / image.Height;
                        }

                        //DrawImage参数根据打印机和图片大小自行调整
                        System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
                        e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);

                        if (printNum < fileList.Count - 1)
                        {
                            printNum++;
                            e.HasMorePages = true;//HasMorePages为true则再次运行PrintPage事件
                            return;
                        }
                        e.HasMorePages = false;
                    }
                    catch { }
                }
            }
        }

        #endregion

        #region 根据bitmap图片打印

        public Bitmap bitmap;
        public List<Bitmap> bitmaps = new List<Bitmap>();

        public void PrintBitmap(bool isPreview = false)
        {
            PrintDocument docToPrint = new PrintDocument();
            docToPrint.BeginPrint += new PrintEventHandler(this.BitmapToPrint_BeginPrint);
            docToPrint.EndPrint += new PrintEventHandler(this.BitmapToPrint_EndPrint);
            docToPrint.PrintPage += new PrintPageEventHandler(this.BitmapToPrint_PrintPage);
            docToPrint.DefaultPageSettings.Landscape = false;

            var printDialog = new PrintDialog();
            printDialog.AllowSomePages = true;
            printDialog.ShowHelp = true;
            printDialog.Document = docToPrint;
            if (isPreview)
            {
                if (printDialog.ShowDialog() == DialogResult.OK)
                {
                    docToPrint.Print();
                }
            }
            else
            {
                docToPrint.Print();
            }
        }
        private void BitmapToPrint_BeginPrint(object sender, PrintEventArgs e)
        {
            if (bitmaps.Count == 0) bitmaps.Add(bitmap);
        }

        private void BitmapToPrint_EndPrint(object sender, PrintEventArgs e)
        {

        }

        private void BitmapToPrint_PrintPage(object sender, PrintPageEventArgs e)
        {
            //图片抗锯齿
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            try
            {
                Bitmap image = bitmaps[printNum];
                int x = 0;
                int y = 0;
                int width = image.Width;
                int height = image.Height;
                if ((width / e.PageBounds.Width) > (height / e.PageBounds.Height))
                {
                    width = e.PageBounds.Width;
                    height = image.Height * e.PageBounds.Width / image.Width;
                }
                else
                {
                    height = e.PageBounds.Height;
                    width = image.Width * e.PageBounds.Height / image.Height;
                }

                //DrawImage参数根据打印机和图片大小自行调整
                Rectangle destRect = new Rectangle(x, y, width, height);
                e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);

                if (printNum < bitmaps.Count - 1)
                {
                    printNum++;
                    e.HasMorePages = true;//HasMorePages为true则再次运行PrintPage事件
                    return;
                }
                isSucPrint = true;
                e.HasMorePages = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                isSucPrint = false;
            }
        }

        #endregion

    }
}